<?php
    if ( ! defined('BASEPATH')) 
        exit('No direct script access allowed');
    class Facility extends CCMIS_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function index()
        {
            $this->setup_view('pfmis/pfmis/facility/view');
        }
        public function addfacility()
        {   
            if($this->input->server('REQUEST_METHOD') == 'GET')
                $this->setup_view('pfmis/pfmis/facility/addfacility');
            else
            {
                $this->load->database();
                $mrNumber = $_POST['MR'];
                for($i = 0; $i < $_POST['add']; $i++,$mrNumber)
                {
                    $getMaxFacilityIdQ = 'SELECT MAX(FacilityID) as FacilityID FROM pf_facilities';
                    $getMaxFacilityIdR = $this->db->query($getMaxFacilityIdQ);
                    $getMaxFacilityId = $getMaxFacilityIdR->result();
                    $addFacilityQ = 'INSERT INTO pf_facilities VALUES (\''.(($getMaxFacilityId[0]->FacilityID) + 1).'\',\''.$_POST['name'].'\',\''.$_POST['type'].'\',\'1\',\''.$_POST['room'].'\',\''.$mrNumber.'\')';
                    $this->db->query($addFacilityQ);                    
                }
                $getMaxExpenditureIdQ = 'SELECT MAX(ExpenditureID) as ExpenditureID FROM pf_expenditure';
                $getMaxExpenditureIdR = $this->db->query($getMaxExpenditureIdQ);
                $getMaxExpenditureId = $getMaxExpenditureIdR->result();
                $addExpenditureQ = 'INSERT INTO pf_expenditure VALUES (\''.(($getMaxExpenditureId[0]->ExpenditureID) + 1).'\',\''.(($getMaxFacilityId[0]->FacilityID) + 1).'\',\'ADD\',\''.date('Y-m-d').'\',\''.$_POST['add'].'\',\''.$_POST['price'].'\',\''.$_SESSION['username'].'\')';
                $this->db->query($addExpenditureQ);
                $this->setup_view('pfmis/pfmis/facility/view');
            }
        }
    }
?>